//
//  Gift_LabelView.m
//  MindBox
//
//  Created by MS on 16-1-9.
//  Copyright (c) 2016年 Lemon. All rights reserved.
//

#import "Gift_LabelView.h"
#import "UIView+Frame.h"
#import "UIImageView+WebCache.h"
#import "Gift_LabeiCell.h"
@interface Gift_LabelView()
@property (nonatomic,strong) NSMutableArray * itemArray;
@end

@implementation Gift_LabelView
{
    UILabel * _titleView;
}


-(void)setGiftPageData:(GiftPageData *)giftPageData
{
    _giftPageData = giftPageData;
    _titleView.text = giftPageData.label_title;
    
    NSArray * labels = giftPageData.label;
    
    [self.itemArray removeAllObjects];
    
     int i=0;
    for (Gift_Label * label in labels) {
        
        Gift_LabeiCell * itemView = [Gift_LabeiCell gift_labelCellWithXib];
        itemView.clipsToBounds = YES;
        [self addSubview:itemView];
        [self.itemArray addObject:itemView];
        
        [itemView.imageView sd_setImageWithURL:[NSURL URLWithString:label.image_url]];
 
        itemView.titleView.text = label.name;

        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewDidCick:)];
        [itemView addGestureRecognizer:tap];
       
        itemView.tag = i;
        i++;
    }
    
}

-(void)viewDidCick:(UIGestureRecognizer *)tap
{
    if (self.LabelViewIndexBlock) {
        _LabelViewIndexBlock(tap.view.tag);
    }
}

-(NSMutableArray *)itemArray
{
    if (_itemArray==nil) {
        _itemArray = [[NSMutableArray alloc] init];
    }
    return _itemArray;
}

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = [UIColor whiteColor];

        _titleView = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, self.width, 30)];
        _titleView.font = [UIFont systemFontOfSize:11];
        [self addSubview:_titleView];
    }
    return self;
}


-(void)layoutSubviews
{
    [super layoutSubviews];
    
    _titleView.frame =CGRectMake(10, 0, self.width, 30);
    
    CGFloat y0 = CGRectGetMaxY(_titleView.frame);
    CGFloat w = self.width/self.itemArray.count;
    CGFloat h = self.height -y0;
    
    int i=0;
    for (Gift_LabeiCell * itemView in self.itemArray) {
        CGFloat x = w * i;
        itemView.frame = CGRectMake(x, y0, w, h);
     
        i++;
    }
   
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
